using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using WindowsApplication4.Properties;
using System.IO;

namespace WindowsApplication4
{
    public class Compaigns
    {
        public class CONSTS
        {

            public class dtCompaigns
            {
                public class Columns
                {
                    public static int CompaignIDIndex = 0;
                    public static int NameIndex = 1;
                    public static int DescriptionIndex = 2;

                }
            }
            public class dtTargets
            {
                public class Columns
                {
                    public static int TargetIDIndex = 0;
                    public static int CompaignIDIndex = 1;
                    public static int VillageIDIndex = 2;
                    public static int DescriptionIndex = 3;
                    public static int LaunchedIndex = 4;
                    

                }
            }

            public class dtAttacks
            {
                public class Columns
                {
                    public static int AttackIDIndex = 0;
                    public static int TargetIDIndex = 1;
                    public static string TargetIDName = "TargetID";
                    public static int AttackingVillageIDIndex = 2;
                    public static int UnitIndex = 3;
                    public static int DescriptionIndex = 4;
                    public static int PriorityIndex = 5;
                    public static string PriorityName = "Priority";
                    public static int TimeLaunchedIndex = 6;
                    public static string TimeLaunchedName = "TimeLaunched";
                    public static int AttackLaunchedIndex = 7;
                }
            }


            public class Relations
            {
                public static string CompaignToTarget = "CompaignToTarget";
                public static string TargetToAttack = "TargetToAttack";
            }

          //  public static string File = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @".\TW++ Saved Files\w3compaigns.xml";
            public static string File
            {
                get
                {
                      string worldfolder = "World" + WindowsApplication4.Properties.Settings.Default.world;
                        string filename = "Compagin" + WindowsApplication4.Properties.Settings.Default.world;
                        string servername = Common.GetServerName(WindowsApplication4.Properties.Settings.Default.Servertype) + ".";
                        return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\TW++ Saved Files\" + servername + worldfolder + @"\" + filename + ".xml";
                   
                   // Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @".\TW++ Saved Files\w3compaigns.xml";
                   
                }
              
            }

        }



        private System.Collections.Generic.List<Compaign> compaigns;

        public  DataSet dsCompaign;
        public  DataTable dtCompaigns;
        public  DataTable dtTargets;
        public  DataTable dtAttacks;
        
        public Compaigns()
        {
            DataColumn col;

            dsCompaign = new DataSet();
            dtCompaigns = new DataTable("Compaigns");
            col = dtCompaigns.Columns.Add("CompaignID", System.Type.GetType("System.Int32"));
            col.AllowDBNull = false;
            col.AutoIncrement = true;
            col.AutoIncrementSeed = -10;
            col.AutoIncrementStep = -1;
            dtCompaigns.Columns.Add("Name", System.Type.GetType("System.String"));
            dtCompaigns.Columns.Add("Description", System.Type.GetType("System.String"));

            dtTargets = new DataTable("Targets");
            col = dtTargets.Columns.Add("TargetID", System.Type.GetType("System.Int32"));
            col.AllowDBNull = false;
            col.AutoIncrement = true;
            col.AutoIncrementSeed = -10;
            col.AutoIncrementStep = -1;
            dtTargets.Columns.Add("CompaignID", System.Type.GetType("System.Int32"));
            dtTargets.Columns.Add("VillageID", System.Type.GetType("System.Int32"));
            dtTargets.Columns.Add("Description", System.Type.GetType("System.String"));
            col = dtTargets.Columns.Add("Launched", System.Type.GetType("System.Boolean"));
            col.DefaultValue = false;

            dtAttacks = new DataTable("Attacks");
            col = dtAttacks.Columns.Add("AttackID", System.Type.GetType("System.Int32"));
            col.AllowDBNull = false;
            col.AutoIncrement = true;
            col.AutoIncrementSeed = -10;
            col.AutoIncrementStep = -1;
            dtAttacks.Columns.Add("TargetID", System.Type.GetType("System.Int32"));
            dtAttacks.Columns.Add("AttackingVillageID", System.Type.GetType("System.Int32"));
            dtAttacks.Columns.Add("Unit", System.Type.GetType("System.String"));
            dtAttacks.Columns.Add("Description", System.Type.GetType("System.String"));
            dtAttacks.Columns.Add("Priority", System.Type.GetType("System.Int32"));
            col = dtAttacks.Columns.Add("TimeLaunched", System.Type.GetType("System.DateTime"));
            col.DefaultValue = DateTime.MinValue;
            col = dtAttacks.Columns.Add("AttackLaunched", System.Type.GetType("System.Boolean"));
            col.DefaultValue = false;

            dsCompaign.Tables.Add(dtCompaigns);
            dsCompaign.Tables.Add(dtTargets);
            dsCompaign.Tables.Add(dtAttacks);

            dtCompaigns.PrimaryKey = new DataColumn[] { dtCompaigns.Columns[CONSTS.dtCompaigns.Columns.CompaignIDIndex] };
            dtTargets.PrimaryKey = new DataColumn[] { dtTargets.Columns[CONSTS.dtTargets.Columns.TargetIDIndex] };
            dtAttacks.PrimaryKey = new DataColumn[] { dtAttacks.Columns[CONSTS.dtAttacks.Columns.AttackIDIndex] };

            dsCompaign.Relations.Add("CompaignToTarget", dtCompaigns.Columns[CONSTS.dtCompaigns.Columns.CompaignIDIndex]
                , dtTargets.Columns[CONSTS.dtTargets.Columns.CompaignIDIndex]);
            dsCompaign.Relations.Add("TargetToAttack", dtTargets.Columns[CONSTS.dtTargets.Columns.TargetIDIndex]
                , dtAttacks.Columns[CONSTS.dtAttacks.Columns.TargetIDIndex]);


            if (File.Exists(CONSTS.File))
            {
                try
                {
                    dsCompaign.ReadXml(CONSTS.File, XmlReadMode.IgnoreSchema);
                }
                catch (Exception e)
                {
                    error frmError = new error();
                    frmError.textBox1.Text = e.Message + "\n" + e.StackTrace;
                    frmError.ShowDialog();
                }                
            }
            /*
            //
            // TEST CODE
            //
            DataRow dr = dtCompaigns.Rows.Add(new object[] { null, "Test1", "Test desc" });

            dr = dtTargets.Rows.Add(new object[] { null, dr[0], 64454,"bad bad target" });
            dtAttacks.Rows.Add(new object[] {null, dr[0], 64713, "Ram", "Real attack",0 });

            dr = dtCompaigns.Rows.Add(new object[] { null, "Test2", "Test 2 desc" });

            dr = dtTargets.Rows.Add(new object[] { null, dr[0], 64454, "much better target" });
            dtAttacks.Rows.Add(new object[] { null, dr[0], 64713, "Axe", "almost Real attack", 0 });

            dr = dtTargets.Rows.Add(new object[] { null, dr[0], 64455, "much better target2" });
            dtAttacks.Rows.Add(new object[] { null, dr[0], 64714, "Ram", "clear", 0 });
            dtAttacks.Rows.Add(new object[] { null, dr[0], 64714, "Axe", "clear2", 0 });
            dtAttacks.Rows.Add(new object[] { null, dr[0], 64715, "Nobleman", "almost Real attack3", 1 });
            dtAttacks.Rows.Add(new object[] { null, dr[0], 64716, "Nobleman", "almost Real attack3", 1 });
            dtAttacks.Rows.Add(new object[] { null, dr[0], 64715, "Nobleman", "almost Real attack3", 2 });
            */
            dsCompaign.AcceptChanges();
        }

        public Compaign this[String name]
        {
            get
            {
                DataRow[] rows = dtCompaigns.Select("Name = '" + name + "'");

                if (rows.Length > 0)
                {
                    return new Compaign(rows[0], this);
                }
                else
                {
                    return null;
                }
            }
        }

        public Compaign this[int id]
        {
            get
            {
                DataRow row = dtCompaigns.Rows.Find(id);

                if (row != null)
                {
                    return new Compaign(row, this);
                }
                else
                {
                    return null;
                }
            }
        }

        public System.Collections.Generic.List<Compaign> AllCompaigns
        {
            get
            {
                if (dtCompaigns.Rows.Count == 0)
                {
                    return new List<Compaign>(0);
                }
                else
                {
                    List<Compaign> compaigns = new List<Compaign>(dtCompaigns.Rows.Count);


                    DataRow[] drs = dtCompaigns.Select(); // use select to make sure we do not get deleted rows.
                    foreach (DataRow dr in drs)
                    {
                        compaigns.Add(new Compaign(dr, this));
                    }

                    return compaigns;
                }
            }
        }


        public Compaign Add(string name)
        {
            DataRow dr = dtCompaigns.Rows.Add(new object[] { null, name, "" });
            return new Compaign(dr, this);

        }

        public bool AreThereChanges
        {
            get
            {
                return dsCompaign.HasChanges();
            }
        }
        public void Save() 
        {
            dsCompaign.WriteXml(CONSTS.File);
            dsCompaign.AcceptChanges();
        }
    }
}
